package ysq.xyg.web.controller;

import ysq.xyg.base.aspect.BaseLog;
import ysq.xyg.base.util.BaseConstant;

import cn.hutool.core.util.StrUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import ysq.xyg.base.aspect.BaseLog;
import ysq.xyg.base.controller.BaseController;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.web.model.po.WebColumnType;
import ysq.xyg.web.model.po.WebTagContent;
import ysq.xyg.web.model.po.WebTagType;
import ysq.xyg.web.model.vo.WebColumnTypeVo;
import ysq.xyg.web.model.vo.WebTagContentVo;
import ysq.xyg.web.model.vo.WebTagTypeVo;
import ysq.xyg.web.service.WebColumnTypeService;
import ysq.xyg.web.service.WebTagContentService;
import ysq.xyg.web.service.WebTagTypeService;
import ysq.xyg.web.util.WebBaseContent;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 网站模块-网站栏目controller层
 *
 * @author shengQiang yu
 * @date 2019-6-5 22:15:56
 */
@RestController
public class WebColumnTypeController extends BaseController {

    private static Logger log= LoggerFactory.getLogger(WebColumnTypeController.class);
    @Resource
    private WebColumnTypeService webColumnTypeService;
    @Resource
    private WebTagContentService webTagContentService;
    @Resource
    private WebTagTypeService webTagTypeService;
    private WebColumnType webColumnType;


    /**
     * 列表页面
     *
     * @param colType 栏目类型:1、普通文章，2、图片集，3、banner图
     * @author shengQiang yu
     * @date 2019-4-25 16:53:29
     * */
    @RequestMapping(value="/webColumnType/listPage")
    public ModelAndView listPage(String colType){
        ModelAndView modelAndView=new ModelAndView("/web/webColumnTypeList");
        modelAndView.addObject("colType",colType);
        return modelAndView;
    }
    /**
     * 分页查询
     *
     * @param webColumnTypeVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @author shengQiang yu
     * @date 2019-6-5 22:15:56
     */
    @BaseLog(module = "网站模块",className = "网站栏目",methods = "分页查询",businessType = BaseConstant.SYS_BUSINESS_TYPE_GET,operatorType= BaseConstant.SYS_OPERATOR_TYPE_PC)
    @RequestMapping(value = "/webColumnType/page",method = RequestMethod.POST)
    public ReturnEntity page(HttpServletRequest request,WebColumnTypeVo webColumnTypeVo, int pageSize, int pageNum){
        SysUser sysUser=acceptCurrentUser(request);
        webColumnTypeVo.setUserId(sysUser.getId());
        webColumnTypeVo.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_NORMAL);
        ReturnEntity returnEntity = webColumnTypeService.page(webColumnTypeVo,pageSize,pageNum);
        return returnEntity;
    }

    /**
     * 跳转连接
     *
     * @param id 主键id
     * @author shengQiang yu
     * @date 2019-6-5 22:15:56
     */
    @RequestMapping(value = "/webColumnType/getEntity",method = RequestMethod.GET)
    public ModelAndView getEntity(HttpServletRequest request,String id,String colType){
        SysUser sysUser=acceptCurrentUser(request);
        ModelAndView view = new ModelAndView("/web/webColumnTypeUpdateOrSave");
        List<WebTagType> webTagTypeList=webTagTypeService.acceptList(new WebTagTypeVo(WebBaseContent.web_tag_type_lm,BaseConstant.base_show_type_zs,sysUser.getId()));
        if(StrUtil.isNotEmpty(id)){
            webColumnType = webColumnTypeService.selectByPrimaryKey(id);
            List<WebTagContent> webTagContentList=webTagContentService.acceptList(new WebTagContentVo("",WebBaseContent.web_tag_type_lm,id,sysUser.getId()));
            if(webTagContentList!=null&&webTagContentList.size()>0){
                for (WebTagContent webTagContent:webTagContentList){
                    if(StrUtil.isEmpty(webColumnType.getTagIds())){
                        webColumnType.setTagIds(webTagContent.getTagId());
                    }else{
                        webColumnType.setTagIds(webColumnType.getTagIds()+","+webTagContent.getTagId());
                    }
                }
            }
            view.addObject("webColumnType", webColumnType);
        }
        view.addObject("webTagTypeList", webTagTypeList);
        view.addObject("colType", colType);
        return view;
    }
    /**
     * 删除
     *
     * @param id 主键id
     * @author shengQiang yu
     * @date 2019-6-5 22:15:56
     */
    @BaseLog(module = "网站模块",className = "网站栏目",methods = "删除",businessType = BaseConstant.SYS_BUSINESS_TYPE_DELETE,operatorType= BaseConstant.SYS_OPERATOR_TYPE_PC)
    @RequestMapping(value = "/webColumnType/delete")
    public ReturnEntity delete(HttpServletRequest request,String id){
        return webColumnTypeService.delete(id,acceptCurrentUser(request));
    }

    /**
     * 添加或修改
     *
     * @param webColumnTypeVo Vo对象
     * @author shengQiang yu
     * @date 2019-6-5 22:15:56
     */
    @BaseLog(module = "网站模块",className = "网站栏目",methods = "添加或修改",businessType = BaseConstant.SYS_BUSINESS_TYPE_UPDATE,operatorType= BaseConstant.SYS_OPERATOR_TYPE_PC)
    @RequestMapping(value = "/webColumnType/addOrUpdate",method = RequestMethod.POST)
    public ReturnEntity delete(HttpServletRequest request,WebColumnTypeVo webColumnTypeVo){
        return webColumnTypeService.addOrUpdate(webColumnTypeVo,acceptCurrentUser(request));
    }
    /**
     * 获取下拉框集合
     *
     * @param code code值
     * @author shengQiang yu
     * @date 2019-4-25 16:49:54
     */
    @RequestMapping(value = "/webColumnType/acceptSelect",method = RequestMethod.POST)
    public ReturnEntity acceptSelect(String colType,String code){
        return webColumnTypeService.acceptSelect(colType,code);
    }
}